package com.kly.cashmall.popup;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.kly.cashmall.R;
import com.kly.cashmall.module.web.WebViewActivity;

import razerdp.basepopup.BasePopupWindow;

public class SystemTipWithTitlePopup extends BasePopupWindow {

    private TextView popup_message;
    private TextView popup_confirm;
    private TextView popup_cancel;
    private TextView popup_title;
    private Context mContext;

    public SystemTipWithTitlePopup(Context context) {
        super(context);
        mContext = context;
        setContentView(R.layout.popup_system_message_with_title);
        popup_message = findViewById(R.id.popup_message);
        popup_confirm = findViewById(R.id.popup_confirm);
        popup_cancel = findViewById(R.id.popup_cancel);
        popup_title = findViewById(R.id.popup_title);

        getContentView().findViewById(R.id.popup_cancel).setOnClickListener(view -> {
            dismiss();
        });

    }

    public void setConfirmClick(View.OnClickListener listener) {
        getContentView().findViewById(R.id.popup_confirm).setOnClickListener(listener);
    }

    public void setCancelClick(View.OnClickListener listener) {
        getContentView().findViewById(R.id.popup_cancel).setOnClickListener(listener);
    }

    public void setCancelVisibility(boolean visibility) {
        getContentView().findViewById(R.id.popup_cancel).setVisibility(visibility ? View.VISIBLE : View.GONE);
    }

    public void setMessageText(String text) {
        popup_message.setText(text);
    }

    public void setPrivacyText(String text, String registerUrl, String privacyUrl) {
        popup_message.setText(text);
        String registerAgreement = text;
//        String termsConditions =  mContext.getString(R.string.login_terms_conditions);
//        String privacyPolicy =  mContext.getString(R.string.privacy_policy);
        String termsConditions = mContext.getString(R.string.login_terms_conditions);
        String privacyPolicy = mContext.getString(R.string.privacy_policy);
        if (registerAgreement.contains(termsConditions) && registerAgreement.contains(privacyPolicy)) {

            int tcStart = registerAgreement.indexOf(termsConditions);
            int tcEnd = tcStart + termsConditions.length();

            int ppStart = registerAgreement.indexOf(privacyPolicy);
            int ppEnd = ppStart + privacyPolicy.length();

            SpannableString spannable = new SpannableString(registerAgreement);

            spannable.setSpan(new ClickableSpan() {
                @Override
                public void onClick(@NonNull View view) {
                    mContext.startActivity(WebViewActivity.callIntent(mContext, mContext.getString(R.string.login_terms_conditions),
                            registerUrl));
                }
            }, tcStart, tcEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            spannable.setSpan(new ClickableSpan() {
                @Override
                public void onClick(@NonNull View widget) {
                    mContext.startActivity(WebViewActivity.callIntent(mContext, mContext.getString(R.string.privacy_policy),
                            privacyUrl));
                }
            }, ppStart, ppEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            popup_message.setLinkTextColor(ContextCompat.getColor(mContext, R.color.color_FD4C5C));
            popup_message.setText(spannable);
            popup_message.setMovementMethod(LinkMovementMethod.getInstance());
        }

    }

    public void setConfirmText(String text) {
        popup_confirm.setText(text);
    }

    public void setCancelText(String text) {
        popup_cancel.setText(text);
    }

    public void setTitleText(String text) {
        popup_title.setText(text);
    }

    public void setTitleTextSize(int size) {
        popup_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    }

    public void setMessageTextSize(int size) {
        popup_message.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
    }

}
